package com.yan.shopping.service;

import com.yan.shopping.dao.DateProduct;
import com.yan.shopping.domain.Product;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Scanner;

public class Mallservicesimpl implements MallServices{
    public static final Scanner INPUT = new Scanner(System.in);

    @Override
    public void buy() {
        System.out.println("商品编号\t\t商品名称\t\t商品价格\t\t商品库存");
        for (int i = 0; i < DateProduct.proList.size(); i++) {
            System.out.println(DateProduct.proList.get(i));
        }
        inner:
        while (true) {
        System.out.println("请输入商品编号");
        String choice = INPUT.next();
        for (int i = 0; i < DateProduct.proList.size(); i++) {
            if(choice.equals(DateProduct.proList.get(i).getId())){
                    System.out.println("请输入购买数量");
                    Integer num = INPUT.nextInt();
                    if(num < 0){
                        System.out.println("购买数量不能为负数");
                        continue inner;
                    }else{
                        if(num > DateProduct.proList.get(i).getStock()){
                            System.out.println("数量不能超过" +  DateProduct.proList.get(i).getStock());
                            continue inner;
                        }else{
                            System.out.println("您还要继续购买吗");
                            String judge = INPUT.next();
                            if("y".equals(judge)){
                                Product product = new Product(DateProduct.proList.get(i).getId() ,DateProduct.proList.get(i).getName() , DateProduct.proList.get(i).getPrice() , num);
                                DateProduct.proList.get(i).setStock(DateProduct.proList.get(i).getStock() - num);
                                DateProduct.carList.add(product);
                                continue inner;
                            }else{
                                Product product = new Product(DateProduct.proList.get(i).getId() ,DateProduct.proList.get(i).getName() , DateProduct.proList.get(i).getPrice() , num);
                                DateProduct.proList.get(i).setStock(DateProduct.proList.get(i).getStock() - num);
                                DateProduct.carList.add(product);
                                break inner;
                            }
                        }
                    }
                }
            }
            System.out.println("商品编号不存在");
            break;
        }
    }



    @Override
    public void shoppingCart() {
        BigDecimal money = new BigDecimal(0.0);
        System.out.println("商品编号\t\t商品名称\t\t商品价格\t\t购买数量");
        for (int i = 0; i < DateProduct.carList.size(); i++) {
            System.out.println(DateProduct.carList.get(i));
            money = money.add(DateProduct.proList.get(i).getPrice().multiply(new BigDecimal(DateProduct.proList.get(i).getStock())));
        }
        System.out.println("总金额为:");
        System.out.println(money.setScale(2 , RoundingMode.HALF_UP));
        System.out.println("是否购买y");
        String choice = INPUT.next();
        while ("y".equals(choice)){
            System.out.println("输入付款金额");
            BigDecimal Umoney= new BigDecimal(INPUT.nextDouble());
            if(Umoney.compareTo(money) < 0){
                System.out.println("金额不足");
            }else{
                System.out.println("找零" + (Umoney.subtract(money)).setScale(2 , RoundingMode.HALF_UP));

                DateProduct.carList.clear();
                break;
            }
        }

    }

    @Override
    public void personInformation() {

    }
}
